<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
session_start(); //we need to call PHP's session object to access it through CI

class Home_Controller extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('result_model', '', 'TRUE');
        $this->load->model('user', '', true);

        if (!$this->session->userdata('logged_in')) {
            redirect('login', 'refresh');
        }

        $this->load->library('authentication');
        $this->load->helper('t');
        $this->loggedUser = $this->authentication->getLoggedUser();
        $this->authentication->redirectIfNotBe(array('SS', 'KAS'));
    }

    function index() {
        // load danh sách rsm/kam
//        $this->load->model('region');
//        $regions = $this->region->get_list();
//        $regionDic = array();       
//        foreach ($regions as $key => $value)
//        {
//           $regionDic[$value['rsm_code']]= $value['region_name'];
//        }
//        $viewData['regionDic'] = $regionDic;
        
        $tlList = $this->user->get_list_by_title(array('NRTL', 'BSTL', 'PGTL'), array('status' => 'active', 'supervisor_code' => $this->loggedUser->code));
        $viewData['tlList'] = $tlList;

        // load todo list
        $this->load->model('evaluation');
        $e = $this->evaluation->get_last();

        $this->load->model('todo');
        $todoList = $this->todo->get_list_by_title_and_evaluation(strtolower($this->loggedUser->title), $e['id']);
        foreach ($todoList as &$each) {
            if (time() < strtotime($each['start_day'])) {
                $each['status'] = 'coming';
            } else if (time() <= strtotime($each['end_day'])) {
                $each['status'] = 'current';
            } else {
                $each['status'] = 'done';
            }
        }
        $viewData['todoList'] = $todoList;

        $this->load->view(get_folder($this->loggedUser->title) . '/home_view', $viewData);
    }

    function settings() {
        
        
        $this->load->view(get_folder($this->loggedUser->title) . '/settings', $viewData);
    }

    function logout() {
        $this->load->helper('cookie');
        $this->session->unset_userdata('logged_in');
        $institute_id = $this->session->userdata('institute_id');
        $this->session->sess_destroy();
        session_destroy();
        delete_cookie("rid");
        redirect('login/index/' . urlencode(base64_encode($institute_id)), 'refresh');
    }

}
?>


